﻿using DotnetCore.Module.Extension;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using MQTT.Server.App;
using MQTT.Share.Extension;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using MQTT.Server.Extension;


namespace MQTT.Server.Host
{
    public class Startup
    {
        public IConfiguration Configuration { get; }
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;

        }

        public void ConfigureServices(IServiceCollection services)
        {

            services.AddMQTT();
            services.AddMqttTcpServerAdapter();
            services.AddMqttWebSocketServerAdapter();

            services.AddDcModule<MqttServerHost>(typeof(MqttServerModule), () =>
            {
                
               
            });
            services.AddMvc();
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env,ILogger<Startup> logger)
        {
            
            app.UseWebsocketMQTTServer(Configuration);
            app.UseMcModule((manager) => { });
            app.UseExceptionHandler(builder =>
            {
                logger.LogInformation("123465487984");
                builder.Run(async context => await ErrorEvent(context));
            });
            app.UseMvc();
        }

        public async Task ErrorEvent(HttpContext context)
        {
            var feature = context.Features.Get<IExceptionHandlerFeature>();
            var error = feature?.Error;
            await new TaskFactory().StartNew(() =>
            {

            });
        }
    }
}
